package com.ifast.hnzk.controller;


import java.util.Arrays;
import java.util.Date;

import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.hnzk.domain.NoteDO;
import com.ifast.hnzk.service.NoteService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2019-06-08 22:58:04 | wch</small>
 */
@Controller
@RequestMapping("/hnzk/note")
public class NoteController extends BaseController {
	@Autowired
	private NoteService noteService;
	
	@GetMapping()
	@RequiresPermissions("note:note:note")
	String Note(){
		return "hnzk/note/note";
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("note:note:note")
	public Result<Page<NoteDO>> list(NoteDO noteDTO){
		EntityWrapper<NoteDO> wrapper = new EntityWrapper<NoteDO>();
		wrapper.like("nickname", noteDTO.getNickname());
		wrapper.like("companyname", noteDTO.getCompanyname());
		wrapper.eq(noteDTO.getStatus() !=null,"status", noteDTO.getStatus());

		wrapper.orderBy("id", false);
        Page<NoteDO> page = noteService.selectPage(getPage(NoteDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("note:note:add")
	String add(){
	    return "hnzk/note/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("note:note:edit")
	String edit(@PathVariable("id") Long id,Model model){
		NoteDO note = noteService.selectById(id);
		model.addAttribute("note", note);
	    return "hnzk/note/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("note:note:add")
	public Result<String> save( NoteDO note){
		note.setCreatetime(new Date());
		noteService.insert(note);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("note:note:edit")
	public Result<String>  update( NoteDO note){
		boolean update = noteService.updateById(note);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("note:note:remove")
	public Result<String>  remove( Long id){
		noteService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("note:note:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		noteService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
